package com.gz.service.impl;

import com.gz.entity.Vo.UserVo;
import com.gz.exception.Assert;
import cn.hutool.crypto.digest.BCrypt;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gz.exception.BusinessCode;
import com.gz.common.UserStatus;
import com.gz.entity.Dto.UserDto;
import com.gz.entity.User;
import com.gz.service.UserService;
import com.gz.mapper.UserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

/**
* @author 龙龙与贝儿
* @description 针对表【t_user】的数据库操作Service实现
* @createDate 2025-05-15 10:16:55
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{
    @Autowired
    UserMapper userMapper;
    @Autowired
    private RedisTemplate redisTemplate;


    @Override
    public ResponseEntity doLogin(UserDto userDto) {
        //获取验证码的key
        String key = userDto.getKey();
        //得到Redis中验证码
        String code = redisTemplate.opsForValue().get(key).toString();
        //判断验证码是否过期
        Assert.error(code==null, BusinessCode.INVALID_VERIFICATION);
//        if (code == null) {
//            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("验证码已过期");
//        }

        //判断验证码是否正确
        Assert.error(!code.equalsIgnoreCase(userDto.getCode()), BusinessCode.VERIFICATION_ERROR);
//        if (!code.equalsIgnoreCase(userDto.getCode())) {
//            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("验证码错误");
//        }
        //删除Redis中的验证码,每次验证后验证码会被删除；
        redisTemplate.delete(key);

        LambdaQueryWrapper<User> wrapper = Wrappers.lambdaQuery(User.class).eq(User::getAccount, userDto.getAccount());
        User user = userMapper.selectOne(wrapper);

        Assert.error(user==null, BusinessCode.USER_NOT_EXIST);
//        if (user==null) {
//            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("用户不存在！");
//        }
        BCrypt encoder=new BCrypt();
        boolean match = encoder.checkpw(userDto.getPassword(), user.getPassword());
        Assert.error(!match, BusinessCode.PASSWORD_ERROR);
//        if (!match) {
//            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("密码不正确！");
//        }

        Assert.error(user.getState()!= UserStatus.ACTIVE.getKey(),BusinessCode.USER_FROZEN_ERROR);
//        if (user.getState()!= UserStatus.ACTIVE.getKey()){
//            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("状态锁定，无法登陆！");
//
//        }
        return ResponseEntity.ok(user);
    }

    @Override
    public UserVo findUserById(Long id) {
        User user = userMapper.selectById(id);
        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(user, userVo);
        return userVo;
    }

    @Override
    public User findUserByMobile(String mobile) {
        LambdaQueryWrapper<User> wrapper = Wrappers.lambdaQuery(User.class).eq(User::getMobile, mobile);
        return userMapper.selectOne(wrapper);
    }

    @Override
    public boolean isEmailExist(String email) {
        LambdaQueryWrapper<User> wrapper = Wrappers.lambdaQuery(User.class).eq(User::getEmail, email);
        return userMapper.selectOne(wrapper) != null;
    }

    @Override
    public boolean isAccountExist(String account) {
        LambdaQueryWrapper<User> wrapper = Wrappers.lambdaQuery(User.class).eq(User::getAccount, account);
        return userMapper.selectOne(wrapper)!= null;
    }

    @Override
    public boolean isMobileExist(String mobile) {
        LambdaQueryWrapper<User> wrapper = Wrappers.lambdaQuery(User.class).eq(User::getMobile, mobile);
        return userMapper.selectOne(wrapper)!= null;
    }

    @Override
    public User register(User user) {
        //对密码进行加密
        user.setPassword(BCrypt.hashpw(user.getPassword()));
        //设置用户状态
        user.setState(UserStatus.ACTIVE.getKey());
        //写入数据库，会自动更新user的id值
        userMapper.insert(user);
        return user;
    }


}




